 layui.config({
 			base: './../../layui/lay/modules/'
 	});
 

layui.use(['form','layer','laydate','table','laytpl','notice'],function(){
		var form = layui.form,
			layer = parent.layer === undefined ? layui.layer : top.layer,
			$ = layui.jquery,
			laydate = layui.laydate,
			laytpl = layui.laytpl,
			notice=layui.notice,
			table = layui.table;
		
	const tableVue=new Vue({
			el: '#rankingList',
			data:{
				getRankingUrl:global_url+"/admin/ranking/getActivty/",
				all_data:null,
				tableIns:null
			},
			methods:{
				//全部
				getAllRanking:function(){
					this.$http.post(this.getRankingUrl+'all')
						.then(
						response=>{
							this.all_data=response.body.datas
							this.tablefill()
						},					
						response=>{
						
						})
				},
				//禁止
				getDisableRanking:function(){
					this.$http.post(this.getRankingUrl+'disable')
						.then(
						response=>{
							this.all_data=response.body.datas
							this.tablefill()
						},					
						response=>{
						
						})
				},
				//激活
				getEnableRanking:function(){
					this.$http.post(this.getRankingUrl+'enable')
						.then(
						response=>{
							this.all_data=response.body.datas
							this.tablefill()
						},					
						response=>{
						
						})
				},
				updataRanking:function(data){
					this.$http.post(global_url+'/admin/ranking/updata',data)
						.then(
						response=>{
							notice.init({
								type: "default",
								autoClose: true,
								title: response.body.msg,
								desktop: true,
							});
							if(response.body.msg=='success'){
								this.getAllRanking();
							}
						},					
						response=>{
							notice.init({
								type: "danger",
								autoClose: true,
								title: "更新失败",
								desktop: true,
							});
						})
				},
				delteRanking:function(id){
					this.$http.post(global_url+'/admin/ranking/delte/'+id)
						.then(
						response=>{
							notice.init({
								type: "default",
								autoClose: true,
								title: response.body.msg,
								desktop: true,
							});
							if(response.body.msg=='success'){
								this.getAllRanking();
							}
						},					
						response=>{
							notice.init({
								type: "danger",
								autoClose: true,
								title: "删除失败",
								desktop: true,
							});
						})
				},
				tablefill:function(){
					this.tableIns = table.render({
						elem: '#rankingList',
						cellMinWidth : 95,
						page : true,
						height : "full-125",
						id : "rankingListTable",
						cols : [[
							{field: 'rtid', title: '表ID', width:60, align:"center"},
							{field: 'rankingTableName', title: '表名称', align:"center",templet:function(d){
								if(d.rankingTableName==null){
									return '不存在该表'
								}
								return d.rankingTableName.tableAsName
							}},
							{field: 'name', title: '排行榜名称', align:"center", edit: 'text'},
							{field: 'rule', title: '排序规则', align:"center",templet:function(d){
								res="大到小"
								if(d.rule=="asc"){
									res='小到大'
								}
								return res
							}},
							{field: 'top', title: '数量', width:60,align:'center',edit: 'text'},
							{field: 'state', title: '状态',align:'center',templet:function(d){
								res="正常"
								if(d.state==0){
									res="禁止"
								}
								return res
							}},
							{title: '操作',  templet:'#rankingListBar',fixed:"right",align:"center"}
							
						]],data:this.all_data
					});
					
				}
				
		}});
		
		
	tableVue.getAllRanking()		
		
	//列表操作
	table.on('tool(rankingList)', function(obj){
		var layEvent = obj.event,
			data = obj.data;
		if(layEvent === 'edit'){ //编辑
			if(data.top>20){
				layer.msg('top的值不能超过20！')
				return
			}
			
			tableVue.updataRanking(data)
	
		} else if(layEvent === 'state_btn'){ //改状态
			sendData={
				"rtid":data.rtid,
				"state":1
			}
			if(data.state==1){
				sendData.state=0
			}
			tableVue.updataRanking(sendData)
		} else if(layEvent === 'sort'){ // 改排序
			sendData={
				"rtid":data.rtid,
				"rule":'desc'
			}
			if(data.rule=='desc'){
				console.log('改变')
				sendData.rule='asc'
			}
			tableVue.updataRanking(sendData)
		}else if(layEvent === 'del'){	//删除
			layer.confirm('确定删除该排行榜？',{icon:3, title:'提示信息'},function(index){
						tableVue.delteRanking(data.rtid)
						layer.close(index);
			});
			
		}
	});
	
	
	//正常按钮点击
	$(".enable_btn"). click(function(){
		tableVue.getEnableRanking()
	})
	
	
	//禁用按钮点击
	$(".disable_btn"). click(function(){
		
		tableVue.getDisableRanking()
	})
	
	
	//查看全部点击
	$(".all_btn"). click(function(){
		tableVue.getAllRanking()
	})
		
	
	$(".as_updata").on("click",function(){
				var index = layui.layer.open({
					title : "别名更改",
					type : 2,
					content : "asName.html",
					success : function(layero, index){
 				
							setTimeout(function(){
									layui.layer.tips('点击此处返回排行榜页面', '.layui-layer-setwin .layui-layer-close', {
											tips: 3
									});
							},500)
					}
				})
				layui.layer.full(index);
				window.sessionStorage.setItem("index",index);
				$(window).on("resize",function(){
						layui.layer.full(window.sessionStorage.getItem("index"));
				})
				
		});
		
	$(".addRanking").on("click",function(){
				var index = layui.layer.open({
					title : "添加",
					type : 2,
					content : "addRanking.html",
					success : function(layero, index){
				
							setTimeout(function(){
									layui.layer.tips('点击此处返回排行榜页面', '.layui-layer-setwin .layui-layer-close', {
											tips: 3
									});
							},500)
					}
				})
				layui.layer.full(index);
				window.sessionStorage.setItem("index",index);
				$(window).on("resize",function(){
						layui.layer.full(window.sessionStorage.getItem("index"));
				})
				
		});
		
	
})